package mn.more.wits.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import net.mygwt.ui.client.Registry;
import net.mygwt.ui.client.Style;
import net.mygwt.ui.client.event.BaseEvent;
import net.mygwt.ui.client.event.SelectionListener;
import net.mygwt.ui.client.widget.Button;
import net.mygwt.ui.client.widget.ContentPanel;

/**
 * @author $Author: mikeliucc $
 * @version $Revision: 119 $
 */
public class MainPanel extends ContentPanel {

	public static final String ID = "main-panel";
	public static final String ID_LOGOUT = "logout-button";

	private static final AppMsg APP_MSG = AppMsg.Self.get();
	private Button btnLogout = new Button("Log Out", new LogoutListener());

	private class LogoutListener implements SelectionListener {

		public void widgetSelected(BaseEvent be) {
			AppContext.setStatusMessage(APP_MSG.logoutInprogress());
			AppContext.removeUser();

			AuthProvider.App.getInstance().invalidate(new AsyncCallback() {

				public void onFailure(Throwable caught) {
					AppContext.setStatusError(APP_MSG.logoutImg(), APP_MSG.logoutError(), caught);
					AppContext.showLoginPage();
				}

				public void onSuccess(Object result) {
					AppContext.setStatusMessage(APP_MSG.logoutImg(), APP_MSG.logoutSuccess());
					AppContext.showLoginPage();
				}
			});
		}
	}

	public MainPanel() {
		super(Style.HEADER | Style.FRAME);
		//super(Style.HEADER);

		setId(ID);
		setBorders(true);
		//setScrollEnabled(true);
		setText(AppMsg.Self.get().mainTitle());

		btnLogout.setId(ID_LOGOUT);
		btnLogout.setStyleName("btn-logout");
		btnLogout.setIconStyle("logout-button-icon");
		btnLogout.setWidth("100px");

		Registry.register(ID_LOGOUT, btnLogout);
		Registry.register(ID, this);
	}

	public void showLogout() { getHeader().addWidget(btnLogout); }

	public void hideLogout() { getHeader().removeWidget(btnLogout); }
}
